/**
 * Created by MR on 2016/5/19 019.
 */
"use strict";
/*全局变量们*/
var partpagenum = 10, currentpage = 1, totalpage = 1;//每页显示条数,当前页,总页数
var tabledata = [];//表单数据
var filltbdata=[];//过滤的表单数据

/*角色/用户名及初始化*/
$(function () {
	switch (Role) {
		/*学生，可以查看自己课程及实验成绩*/
		case 'student': {
			//设置内容
			getsetstudentContent();
			$("#studentcontainer").fadeIn();
		} break;
		default: {
			// $("#studentcontainer").remove();
			// $("#studentcontainer").html(langThis("role_noRight"));
			// $("#studentcontainer").fadeIn();
		}
	}
});

/*前端逻辑*/
/*控制模态框内容*/
/*垂直居中*/
function centerModals() {
	$('.modal').each(function (i) {
		var $clone = $(this).clone().css('display', 'block').appendTo('body');
		var top = Math.round(($clone.height() - $clone.find('.modal-content').height()) / 3);
		top = top > 0 ? top : 0;
		$clone.remove();
		$(this).find('.modal-content').css("margin-top", top);
	});
}
$('.modal').on('show.bs.modal', centerModals);
$(window).on('resize', centerModals);
/*学生设置成绩面板*/
function getsetstudentContent() {
	$("div.progress.myfakeprogressbar").css("display", "block");
	/*绑定班级查询框变化事件*/
	$('#queryststring').change(function () {
		if ($("#queryststring").attr("data-lessonid")=="false")
		{
			$("#itemTbody_students").html(langThis("sm_coursenotfd"));
			return;
		}

		setscorepanel(currentpage);
	});
	var sucessfct=function(data){
		$("div.progress.myfakeprogressbar").fadeOut();
		if (!data.data || !data.data.length) {
			$("#itemTbody_students").html(langThis("sm_coursenotfd"));
			return;
		}
		tabledata=data.data;
		setscorepanel(currentpage);
	};
	$.ajax({
		url: "chengji_query_studnet.action",
		type: "POST",
		data: { "student_id": UserID },//全局变量，classid
		dataType: "json",
		success: sucessfct,
		error: function (event) {
			$("#itemTbody_students").html(langThis("sm_noScoredata"));
			$("div.progress.myfakeprogressbar").fadeOut(250);
			// console.log(event);
		}
	});
}
$('#modal_studentscore').on('show.bs.modal', function (event) {
	$("#modelloading").show();
	 $("#studentslab").html("");
  var lessonid = $(event.relatedTarget).data('lessonid');
  var lessonname = $(event.relatedTarget).data('lessonname');
  var modal = $(this);
  modal.find("h4.modal-title").text(lessonname);
  var sucessfct=function (data){
	  $("#modelloading").hide();
	  if (!data.data || !data.data.length) {
		  $("#studentslab").html(langThis("sm_coursenotfd"));
		  return;
	  }
	  var linstr="";
	  for(var i in data.data){
		linstr+="<tr><td>"+data.data[i].lab_name+"</td>";
		// linstr+="<td>"+data.data[i].quscore+"</td>";
		// linstr+="<td>"+data.data[i].wsscore+"</td>";
		linstr+="<td>"+data.data[i].totalscore+"</td></tr>";
	  }
	  $("#studentslab").html(linstr);
  };
	$.ajax({
		url: "chengji_query_lesson_student.action",
		type: "POST",
		data: { "student_id": UserID,
				"lesson_id":lessonid
		 },//全局变量，classid
		dataType: "json",
		success: sucessfct,
		error: function (event) {
			$("#itemTbody_students").html(langThis("sm_noScoredata"));
			$("div.progress.myfakeprogressbar").fadeOut(250);
			console.log(event);
		}
	});
});
/*学生设置成绩面板*/
function setscorepanel(ctpage) {
	var filldata = filltbdata.length ? filltbdata : tabledata;
	totalpage = Math.ceil(filldata.length / partpagenum);
	currentpage = ctpage > totalpage ? totalpage : ctpage;
	var tablest="";
	for(var i = (currentpage - 1) * partpagenum; i < filldata.length; i++){
		tablest+="<tr><td>"+filldata[i].lesson_id+"</td>";
		tablest+="<td>"+filldata[i].lesson_name+"</td>";
		tablest+="<td>"+filldata[i].score+"</td>";
		tablest+="<td><a data-lessonid='"+filldata[i].lesson_id+"' data-lessonname='"+filldata[i].lesson_name+"' role='button' class='btn btn-link' data-toggle='modal' data-target='#modal_studentscore'>"+langThis("sc_detail")+"</a></td></tr>";
		if (i == currentpage * partpagenum - 1) break;
	}
	$("#itemTbody_students").html(tablest);
	setpartpagebar();
	// 子页面设置父页面iframe高度
    if(window.parent.setiframeHeight){
        var ht=$("#container").height();
		ht=ht>800?ht:800;
        window.parent.setiframeHeight(ht);
    }
}
function getQuerystname(str) {
	filltbdata=[];
    // 替换特殊字符()
	var re = str ? new RegExp(str.replace(/\(/g,"\\(").replace(/\)/g,"\\)")) : /.*/;
	var lis = "";
	for (var i = 0; i < tabledata.length; i++) {
		if (re.test(tabledata[i].lesson_name)) {
			lis += '<li>' + tabledata[i].lesson_name + '</li>';
			filltbdata.push(tabledata[i]);
		}else if(re.test(tabledata[i].lesson_id)){
			lis += '<li title="'+tabledata[i].lesson_name+'">' + tabledata[i].lesson_id + '</li>';
			filltbdata.push(tabledata[i]);
		}
	}
	$("ul.dropdown-menu.querymenu_st").html(lis);
	$("#queryststring").attr("data-lessonid", 0);
	//无匹配项提示
	if (!$("ul.dropdown-menu.querymenu_st>li").length) {
		$("ul.dropdown-menu.querymenu_st").append(langThis("kc_nomatches"));
		$("#queryststring").attr("data-lessonid", false);
	}

	/*绑定查询下拉列表点击事件*/
	$("ul.dropdown-menu.querymenu_st>li").click(function () {
		var lessonname=$(this).text();
		$("#queryststring").val(lessonname);
		getQuerystname(lessonname);
		setscorepanel(currentpage);
	});
}
function completer_click_st() {
	getQuerystname($("#queryststring").val());
	$("#inputstcontentdiv").attr("class", "input-group-btn open");
}
//分页控制
function setpartpagebar(){
	var pagesli='';
	//首页、上一页
	if(currentpage==1 || totalpage==1){
		pagesli+='<li title="'+langThis("nav_master")+'" class="active"><a>&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'" class="active"><a>&lt;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_master")+'"><a style="cursor:pointer;" onclick="gotofirstpage()">&laquo;</a></li>';
		pagesli+='<li title="'+langThis("nav_pre")+'"><a style="cursor:pointer;" onclick="previouspage()">&lt;</a></li>';
	}
	//页码
	var startpage;
	if (currentpage-5<=0){
		startpage=1;
	}else if(currentpage+5>totalpage){
		startpage=totalpage-9;
	}else{
		startpage=currentpage-4;
	}
	// 左过长
	if (startpage>1){
		pagesli += '<li><a style="cursor:pointer;" onclick="gotopage(' + (startpage-1) + ')">...</a></li>';
	}else{
		startpage=1;
	}
	for(var j=startpage;j<=totalpage;j++){
		if(j==currentpage){
			pagesli+='<li class="active"><a>'+j+'</a></li>';
		}else{
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+j+')">'+j+'</a></li>';
		}
		//右过长
		if((j==startpage+9) && (j<totalpage)){
			pagesli+='<li><a style="cursor:pointer;" onclick="gotopage('+(startpage+10)+')">...</a></li>';
			break;
		}
	}
	//下一页、尾页
	if(currentpage==totalpage || totalpage==1){
		pagesli+='<li title="'+langThis("nav_next")+'" class="active"><a>&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'" class="active"><a>&raquo;</a></li>';
	}else{
		pagesli+='<li title="'+langThis("nav_next")+'"><a style="cursor:pointer;" onclick="nextpage()">&gt;</a></li>';
		pagesli+='<li title="'+langThis("nav_last")+'"><a style="cursor:pointer;" onclick="gotolastpage()">&raquo;</a></li>';
	}
	$("#partpagenavbar>ul.pagination").html(pagesli);
	$("#partpagenavbar").fadeIn();
}
function previouspage(){
	setscorepanel(--currentpage);
}
function nextpage(){
	setscorepanel(++currentpage);
}
function gotopage(pagenum){
	setscorepanel(pagenum);
}
function gotofirstpage(){
	setscorepanel(1);
}
function gotolastpage(){
	setscorepanel(totalpage);
}
// 翻页快捷键
document.onkeydown = function(e){
    if((e||event).keyCode==37 && currentpage>1){//上一页
		previouspage();
	}else if((e||event).keyCode==39 && currentpage<totalpage){//下一页
		nextpage();
	}else if((e||event).keyCode==36 && currentpage !=1){//首页
		gotofirstpage();
	}else if((e||event).keyCode==35 && currentpage != totalpage){//尾页
		gotolastpage();
	}
};